package com.dreamlin.basepro.base.view.activity;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewbinding.ViewBinding;

import com.dreamlin.basepro.App;
import com.dreamlin.basepro.base.viewmodel.BaseViewModel;

public abstract class BaseActivity<VM extends BaseViewModel, Binding extends ViewBinding> extends WithoutVMActivity<Binding> {

    protected VM viewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        viewModel = App.getViewModelFactory().create(getVM());
        initObserver();
        super.onCreate(savedInstanceState);
    }

    protected void initObserver() {
        viewModel.initDialog(this, observer -> {
            if (observer.isShow()) {
                showLoading(observer.getMessage());
            } else {
                hideLoading();
            }
        });

        viewModel.initError(this, error -> {
            toast(error);
            onError();
        });
    }

    protected abstract Class<VM> getVM();

    protected void onError() {
        //TODO:error handle
    }

}
